package com.jack.hhitseat.task;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jack.hhitseat.bean.AppBean;
import com.jack.hhitseat.bean.User;
import com.jack.hhitseat.service.HttpClient;
import com.jack.hhitseat.service.impl.LogServiceImpl;
import com.jack.hhitseat.utils.MyUtils;

public class FirstSeatRunnable extends Thread {
	private final Logger logger = LoggerFactory.getLogger(MyRunnable.class);
	private static HttpClient httpClient = new HttpClient();
	private static LogServiceImpl logServiceImpl = (LogServiceImpl) AppBean.getBean("logServiceImpl");
	private User u;
	private ConcurrentMap<String, String> sessionMap = new ConcurrentHashMap<>();
	private final static String KEY = "0";
	
	private final static  String BOOK_URL = "http://seat.hhit.edu.cn/ClientWeb/pro/ajax/reserve.aspx?" +
			"&dev_id={dev_id}" + "&type={type}" + "&start={start}" + "&end={end}" +
			"&start_time={start_time}" + "&end_time={end_time}"+"&number={number}" + "&act={act}" +
			"&_={_}"; 
	
	public FirstSeatRunnable() {
	}
	
	public FirstSeatRunnable(User u
			, ConcurrentMap<String, String> sessionMap) {
		this.u = u;
		this.sessionMap = sessionMap;
	}
	
	@Override
	public synchronized  void run() {
		String stuNum = u.getStuNum();
		if(logServiceImpl.isSuccess(stuNum) > 0) {
			return;
		}
		String[] seats = u.getSeat().split(",");
		String session = sessionMap.get(stuNum);
		Map<String, Object> qzParparamMap = new HashMap<String, Object>();

    	String yyrq = MyUtils.getNextDate(); 
		qzParparamMap.put("type","dev");
		qzParparamMap.put("start",yyrq+"+08:00");
		qzParparamMap.put("start_time","800"); 
		qzParparamMap.put("end",yyrq+"+22:00");		//正常时间
		qzParparamMap.put("end_time","2200");		//正常时间
		qzParparamMap.put("number", KEY);			//万能钥匙Key
		qzParparamMap.put("act","set_resv");
		
		Date d = MyUtils.String2Date(yyrq);
		  
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;	//预约日期是星期几
		
		final int COUNT = 500;
		String seatNumb = seats[0].split("=")[0];
		String result = "";
		int i = 0;
        qzParparamMap.put("dev_id", seatNumb); 
		String sjc = System.currentTimeMillis()+"";
		qzParparamMap.put("_",sjc);
		
		if(w != 3) {
			for (i = 1; i <= COUNT; i++) {
				result = httpClient.qz2(BOOK_URL, session, qzParparamMap);
				if(!result.contains("验证码")) {
					if(result.contains("成功")) {
						logServiceImpl.addLog(stuNum, seatNumb);
						break;
					}else if(result.contains("已有预约")) {
						break;
					}else if(result.contains("不在开放时间")) {
						break;
					}
					logger.warn("【首位】==={}==={}==={}==={}", i, u.getUserName() ,
							result.split("\"msg\":\"")[1].split("\",\"data\":")[0] , seats[0].split("=")[1]);
					break;
				}else {
//				    String key = AipOcrUtil.getResult(AipOcrUtil.getCode(httpClient.yzm2Api(session)));
//				    qzParparamMap.put("number", key);
					httpClient.yzm(session);
				}
			}
		}else {
			for (i = 1; i <= COUNT; i++) {
				qzParparamMap.put("start",yyrq+"+08:00");
				qzParparamMap.put("start_time","800"); 
				qzParparamMap.put("end",yyrq+"+12:00");
				qzParparamMap.put("end_time","1200");
				result = httpClient.qz2(BOOK_URL, session, qzParparamMap);
				qzParparamMap.put("start",yyrq+"+18:00");
				qzParparamMap.put("start_time","1800"); 
				qzParparamMap.put("end",yyrq+"+22:00");		
				qzParparamMap.put("end_time","2200");		
				result = httpClient.qz2(BOOK_URL, session, qzParparamMap);
				
				if(!result.contains("验证码")) {
					if(result.contains("成功")) {
						logServiceImpl.addLog(stuNum, seatNumb);
						break;
					}else if(result.contains("已有预约")) {
						break;
					}else if(result.contains("不在开放时间")) {
						logger.warn("图书馆系统未开放！！！");
						break;
					}
					logger.warn("【首位】==={}==={}==={}==={}", i, u.getUserName() ,
							result.split("\"msg\":\"")[1].split("\",\"data\":")[0] , seats[0].split("=")[1]);
					break;
				}else {				
//				    String key = AipOcrUtil.getResult(AipOcrUtil.getCode(httpClient.yzm2Api(session)));
//				    qzParparamMap.put("number", key);
					httpClient.yzm(session);
				}
			}
		}
	}
}
